from matplotlib import pyplot as plt
import numpy as np

X1=[-1,-0.6,-0.2,0.2,0.6,1]

P1 = [[0.0384615,-0.121457,0,1.72065],
     [0.1,0.704453,2.06478,-3.31478],
     [0.5,0.765182,-1.91296,3.70074e-16],
     [0.5,-0.765182,-1.91296,3.31478],
     [0.1,-0.704453,2.06478,-1.72065]]

X2=[-1,-0.8,-0.6,-0.4,-0.2,0,0.2,0.4,0.6,0.8,1]

P2 = [[0.0384615, 0.0881415, 0, 0.34171],
      [0.0588235, 0.129147, 0.205026, 0.893259],
      [0.1, 0.318348, 0.740981, 0.836385],
      [0.2, 0.715107, 1.24281, 13.4083],
      [0.5, 2.82122, 9.28777, -54.4694],
      [1, 1.33227e-15, -23.3939, 54.4694],
      [0.5, -2.82122, 9.28777, -13.4083],
      [0.2, -0.715107, 1.24281, -0.836385],
      [0.1, -0.318348, 0.740981, -0.893259],
      [0.0588235, -0.129147, 0.205026, -0.34171]]

X3=[-1,-0.9,-0.8,-0.7,-0.6,-0.5,-0.4,-0.3,-0.2,-0.1,0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1]

P3 = [[0.0384615, 0.0799471, 0, 0.602572],
      [0.0470588, 0.0980243, 0.180772, 0.15456],
      [0.0588235, 0.138815, 0.22714, 0.49523],
      [0.0754717, 0.1991, 0.375709, 0.86119],
      [0.1, 0.300078, 0.634066, 1.58261],
      [0.137931, 0.474369, 1.10885, 3.54357],
      [0.2, 0.802446, 2.17192, 5.72853],
      [0.307692, 1.40869, 3.89048, 12.5344],
      [0.5, 2.56281, 7.65079, -32.7891],
      [0.8, 3.1093, -2.18594, -89.0703],
      [1, 4.44089e-16, -28.907, 89.0703],
      [0.8, -3.1093, -2.18594, 32.7891],
      [0.5, -2.56281, 7.65079, -12.5344],
      [0.307692, -1.40869, 3.89048, -5.72853],
      [0.2, -0.802446, 2.17192, -3.54357],
      [0.137931, -0.474369, 1.10885, -1.58261],
      [0.1, -0.300078, 0.634066, -0.86119],
      [0.0754717, -0.1991, 0.375709, -0.49523],
      [0.0588235, -0.138815, 0.22714, -0.15456],
      [0.0470588, -0.0980243, 0.180772, -0.602572]]

X4=[-1,-0.95,-0.9,-0.85,-0.8,-0.75,-0.7,-0.65,-0.6,-0.55,-0.5,-0.45,-0.4,-0.35,-0.3,-0.25,-0.2,-0.15,-0.1,-0.05,0,0.05,0.1,0.15,0.2,0.25,0.3,0.35,0.4,0.45,0.5,0.55,0.6,0.65,0.7,0.75,0.8,0.85,0.9,0.95,1]

P4 = [[0.0384615, 0.0769914, 0, 1.03366],
      [0.0424403, 0.0847439, 0.155049, -0.0504996],
      [0.0470588, 0.0998701, 0.147474, 0.304036],
      [0.052459, 0.116898, 0.19308, 0.295417],
      [0.0588235, 0.138421, 0.237392, 0.415729],
      [0.06639, 0.165278, 0.299751, 0.546831],
      [0.0754717, 0.199355, 0.381776, 0.740843],
      [0.0864865, 0.243089, 0.492903, 1.01454],
      [0.1, 0.299988, 0.645084, 1.40964],
      [0.116788, 0.375069, 0.856531, 1.98355],
      [0.137931, 0.475599, 1.15406, 2.81865],
      [0.164948, 0.612145, 1.57686, 4.01722],
      [0.2, 0.79996, 2.17944, 5.65783],
      [0.246154, 1.06034, 3.02812, 7.60997],
      [0.307692, 1.42023, 4.16961, 8.93044],
      [0.390244, 1.90416, 5.50918, 6.19921],
      [0.5, 2.50158, 6.43906, -9.41209],
      [0.64, 3.07489, 5.02725, -50.5021],
      [0.8, 3.19885, -2.54806, -99.1679],
      [0.941176, 2.20029, -17.4232, -61.0617],
      [1, -4.44089e-16, -26.5825, 61.0617],
      [0.941176, -2.20029, -17.4232, 99.1679],
      [0.8, -3.19885, -2.54806, 50.5021],
      [0.64, -3.07489, 5.02725, 9.41209],
      [0.5, -2.50158, 6.43906, -6.19921],
      [0.390244, -1.90416, 5.50918, -8.93044],
      [0.307692, -1.42023, 4.16961, -7.60997],
      [0.246154, -1.06034, 3.02812, -5.65783],
      [0.2, -0.79996, 2.17944, -4.01722],
      [0.164948, -0.612145, 1.57686, -2.81865],
      [0.137931, -0.475599, 1.15406, -1.98355],
      [0.116788, -0.375069, 0.856531, -1.40964],
      [0.1, -0.299988, 0.645084, -1.01454],
      [0.0864865, -0.243089, 0.492903, -0.740843],
      [0.0754717, -0.199355, 0.381776, -0.546831],
      [0.06639, -0.165278, 0.299751, -0.415729],
      [0.0588235, -0.138421, 0.237392, -0.295417],
      [0.052459, -0.116898, 0.19308, -0.304036],
      [0.0470588, -0.0998701, 0.147474, 0.0504996],
      [0.0424403, -0.0847439, 0.155049, -1.03366]]

X5=[-1,-0.95,-0.9,-0.85,-0.8,-0.75,-0.7,-0.65,-0.6,-0.55,-0.5,-0.45,-0.4,-0.35,-0.3,-0.25,-0.2,-0.15,-0.1,-0.05,0,0.05,0.1,0.15,0.2,0.25,0.3,0.35,0.4,0.45,0.5,0.55,0.6,0.65,0.7,0.75,0.8,0.85,0.9,0.95,1]

P5 = [[0.0384615, 0.0754823, 0, 1.91691],
      [0.0403785, 0.0790766, 0.143768, -0.31999],
      [0.0424403, 0.085665, 0.119769, 0.304008],
      [0.0446615, 0.0922235, 0.14257, 0.165203],
      [0.0470588, 0.0996617, 0.15496, 0.235237],
      [0.0496509, 0.107851, 0.172603, 0.254572],
      [0.052459, 0.116958, 0.191696, 0.29373],
      [0.0555074, 0.127094, 0.213725, 0.335001],
      [0.0588235, 0.138408, 0.238851, 0.384569],
      [0.062439, 0.151072, 0.267693, 0.44253],
      [0.06639, 0.165286, 0.300883, 0.510996],
      [0.0707182, 0.181288, 0.339208, 0.592018],
      [0.0754717, 0.199359, 0.383609, 0.688251],
      [0.0807062, 0.21983, 0.435228, 0.802918],
      [0.0864865, 0.243097, 0.495447, 0.93998],
      [0.0928882, 0.269631, 0.565945, 1.10429],
      [0.1, 0.299999, 0.648767, 1.30179],
      [0.107926, 0.334878, 0.746402, 1.53969],
      [0.116788, 0.375085, 0.861878, 1.82666],
      [0.126733, 0.421604, 0.998878, 2.17299],
      [0.137931, 0.475623, 1.16185, 2.59052],
      [0.150588, 0.538572, 1.35614, 3.09231],
      [0.164948, 0.612178, 1.58806, 3.69155],
      [0.181303, 0.698503, 1.86493, 4.39922],
      [0.2, 0.799998, 2.19487, 5.21924],
      [0.221453, 0.919527, 2.58632, 6.13942],
      [0.246154, 1.06035, 3.04677, 7.11491],
      [0.274678, 1.22603, 3.58039, 8.0393],
      [0.307692, 1.42013, 4.18334, 8.69662],
      [0.345946, 1.6456, 4.83558, 8.6863],
      [0.390244, 1.90367, 5.48706, 7.3187],
      [0.441379, 2.19174, 6.03596, 3.50003],
      [0.5, 2.5001, 6.29846, -4.31338],
      [0.566372, 2.80694, 5.97496, -17.8852],
      [0.64, 3.07215, 4.63357, -38.3111],
      [0.719101, 3.232, 1.76024, -64.0738],
      [0.8, 3.19987, -3.0453, -88.3889],
      [0.876712, 2.88187, -9.67447, -98.3154],
      [0.941176, 2.21381, -17.0481, -80.08],
      [0.984615, 1.21125, -23.0541, -31.2252],
      [1, -4.10783e-15, -25.396, 31.2252],
      [0.984615, -1.21125, -23.0541, 80.08],
      [0.941176, -2.21381, -17.0481, 98.3154],
      [0.876712, -2.88187, -9.67447, 88.3889],
      [0.8, -3.19987, -3.0453, 64.0738],
      [0.719101, -3.232, 1.76024, 38.3111],
      [0.64, -3.07215, 4.63357, 17.8852],
      [0.566372, -2.80694, 5.97496, 4.31338],
      [0.5, -2.5001, 6.29846, -3.50003],
      [0.441379, -2.19174, 6.03596, -7.3187],
      [0.390244, -1.90367, 5.48706, -8.6863],
      [0.345946, -1.6456, 4.83558, -8.69662],
      [0.307692, -1.42013, 4.18334, -8.0393],
      [0.274678, -1.22603, 3.58039, -7.11491],
      [0.246154, -1.06035, 3.04677, -6.13942,],
      [0.221453, -0.919527, 2.58632, -5.21924],
      [0.2, -0.799998, 2.19487, -4.39922],
      [0.181303, -0.698503, 1.86493, -3.69155],
      [0.164948, -0.612178, 1.58806, -3.09231],
      [0.150588, -0.538572, 1.35614, -2.59052],
      [0.137931, -0.475623, 1.16185, -2.17299],
      [0.126733, -0.421604, 0.998878, -1.82666],
      [0.116788, -0.375085, 0.861878, -1.53969],
      [0.107926, -0.334878, 0.746402, -1.30179],
      [0.1, -0.299999, 0.648767, -1.10429],
      [0.0928882, -0.269631, 0.565945, -0.93998],
      [0.0864865, -0.243097, 0.495447, -0.802918],
      [0.0807062, -0.21983, 0.435228, -0.688251],
      [0.0754717, -0.199359, 0.383609, -0.592018],
      [0.0707182, -0.181288, 0.339208, -0.510996],
      [0.06639, -0.165286, 0.300883, -0.44253],
      [0.062439, -0.151072, 0.267693, -0.384569],
      [0.0588235, -0.138408, 0.238851, -0.335001],
      [0.0555074, -0.127094, 0.213725, -0.29373],
      [0.052459, -0.116958, 0.191696, -0.254572],
      [0.0496509, -0.107851, 0.172603, -0.235237],
      [0.0470588, -0.0996617, 0.15496, -0.165203],
      [0.0446615, -0.0922235, 0.14257, -0.304008],
      [0.0424403, -0.085665, 0.119769, 0.31999],
      [0.0403785, -0.0790766, 0.143768, -1.91691]]


def f(x):
    return 1.0*1/(1 + 25*x*x)

def pf1(x):
    n = len(X1)
    for i in range(1,n):
        if x < X1[i]:
            return P1[i-1][0] + P1[i-1][1]*(x-X1[i-1]) + P1[i-1][2]*(x-X1[i-1])*(x-X1[i-1]) + P1[i-1][3]*(x-X1[i-1])*(x-X1[i-1])*(x-X1[i-1]) - f(x)

def pf2(x):
    n = len(X2)
    for i in range(1,n):
        if x < X2[i]:
            return P2[i-1][0] + P2[i-1][1]*(x-X2[i-1]) + P2[i-1][2]*(x-X2[i-1])*(x-X2[i-1]) + P2[i-1][3]*(x-X2[i-1])*(x-X2[i-1])*(x-X2[i-1]) - f(x)

def pf3(x):
    n = len(X3)
    for i in range(1,n):
        if x < X3[i]:
            return P3[i-1][0] + P3[i-1][1]*(x-X3[i-1]) + P3[i-1][2]*(x-X3[i-1])*(x-X3[i-1]) + P3[i-1][3]*(x-X3[i-1])*(x-X3[i-1])*(x-X3[i-1]) - f(x)

def pf4(x):
    n = len(X4)
    for i in range(1,n):
        if x < X4[i]:
            return P4[i-1][0] + P4[i-1][1]*(x-X4[i-1]) + P4[i-1][2]*(x-X4[i-1])*(x-X4[i-1]) + P4[i-1][3]*(x-X4[i-1])*(x-X4[i-1])*(x-X4[i-1]) - f(x)

#def pf5(x):
#   n = len(X5)
#    for i in range(1,n):
#        if x < X5[i]:
#            return P5[i-1][0] + P5[i-1][1]*(x-X5[i-1]) + P5[i-1][2]*(x-X5[i-1])*(x-X5[i-1]) + P5[i-1][3]*(x-X5[i-1])*(x-X5[i-1])*(x-X5[i-1])
        
x = np.linspace(-1, 1, 100)
y = np.array([f(t) for t in x])
y1 = np.array([pf1(t) for t in x])
y2 = np.array([pf2(t) for t in x])
y3 = np.array([pf3(t) for t in x])
y4 = np.array([pf4(t) for t in x])
#plt.plot(x, y)
plt.plot(x, y1)
plt.plot(x, y2)
plt.plot(x, y3)
plt.plot(x, y4)
plt.savefig("../doc/A2.png")
